package demo4接口使用;

import java.util.Arrays;
import java.util.Comparator;

class Studen implements Comparable<Studen>{
    public String name;
    public  int age;

    public Studen(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Studen{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Studen o) {
        return o.age - this.age;
    }
}

class AgeComparetor implements Comparator<Studen> {
    @Override
    public int compare(Studen o1, Studen o2) {
        return o1.age - o2.age;
    }
}
class NameComparetor implements Comparator<Studen> {
    @Override
    public int compare(Studen o1, Studen o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test1{

    public static void bubbleSort(Comparable[] comparables){
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length - i - 1; j++) {
                if(comparables[j].compareTo(comparables[j + 1]) > 0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args){
        Studen studen1 = new Studen("小明",16);
        Studen studen2 = new Studen("小美",18);

        AgeComparetor ageComparetor1 = new AgeComparetor();
        System.out.println(ageComparetor1.compare(studen1, studen2));

        NameComparetor nameComparetor1 = new NameComparetor();
        System.out.println(nameComparetor1.compare(studen1, studen2));

    }




    public static void main1(String[] args){
        Studen studen1 = new Studen("小明",16);
        Studen studen2 = new Studen("小美",18);
        System.out.println(studen1.compareTo(studen2));



        Studen[] studens = new Studen[3];
        studens[0] = new Studen("小明",1);
        studens[1] = new Studen("小wda ",17);
        studens[2] = new Studen("小sd3",12);

        bubbleSort(studens);
        System.out.println(Arrays.toString(studens));
    }

}
